package com.ebs.weather_spider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ebs.weather_spider.config.GlobalVariables;
import com.ebs.weather_spider.entity.WeatherAlert;
import com.ebs.weather_spider.entity.WeatherCity;
import com.ebs.weather_spider.mapper.WeatherAlertMapper;
import com.ebs.weather_spider.mapper.WeatherCityMapper;
import com.ebs.weather_spider.result.ResultResponse;
import com.ebs.weather_spider.service.WeatherService;
import com.ebs.weather_spider.vo.WeatherAlertVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WeatherServiceImpl implements WeatherService {
    @Resource
    private WeatherCityMapper weatherCityMapper;
    @Resource
    private WeatherAlertMapper weatherAlertMapper;

    @Override
    public ResultResponse<List<WeatherCity>> getWeather() {
        List<WeatherCity> weatherCityList = new LambdaQueryChainWrapper<>(weatherCityMapper)
                .orderByAsc(WeatherCity::getId)
                .list();
        return ResultResponse.success(weatherCityList);
    }

    @Override
    public ResultResponse<List<WeatherAlertVO>> getAlert() {
        List<WeatherAlert> weatherAlertList = new LambdaQueryChainWrapper<>(weatherAlertMapper)
                .ge(WeatherAlert::getIssuetime, DateUtil.today() + " 0:0:0")
                // 不查询预警类型为空的数据
                .isNotNull(WeatherAlert::getType)
                .orderByDesc(WeatherAlert::getIssuetime)
                .list();
        List<WeatherAlertVO> weatherAlertVOList = new ArrayList<>();
        if (!weatherAlertList.isEmpty()) {
            List<WeatherCity> weatherCityList = new LambdaQueryChainWrapper<>(weatherCityMapper)
                    .list();
            for (WeatherAlert weatherAlert : weatherAlertList) {
                WeatherAlertVO weatherAlertVO = new WeatherAlertVO();
                BeanUtils.copyProperties(weatherAlert, weatherAlertVO);
                for (WeatherCity weatherCity : weatherCityList) {
                    if (weatherCity.getArea_code().equals(weatherAlert.getArea_code())) {
                        weatherAlertVO.setGeom(weatherCity.getGeom());
                    }
                }
                for (Map.Entry<String, Integer> alertLevel : GlobalVariables.alertLevelMap.entrySet()) {
                    if (weatherAlert.getLevel().equals(alertLevel.getValue())) {
                        weatherAlertVO.setLevel_name(alertLevel.getKey());
                    }
                }
                for (Map.Entry<String, Integer> alertType : GlobalVariables.alertTypeMap.entrySet()) {
                    if (weatherAlert.getType().equals(alertType.getValue())) {
                        weatherAlertVO.setType_name(alertType.getKey());
                    }
                }
                weatherAlertVOList.add(weatherAlertVO);
            }
        }
        return ResultResponse.success(weatherAlertVOList);
    }
}
